// 交互模块API

import api from '../index';
import {
  ApiResponse,
  LikeToggleRequest,
  DislikeRequest,
  CreateCommentRequest,
  LikeStatusRequest,
  CommentQueryParams,
  CommentListResponse,
  LikeStatusResponse,
} from '../../types';

// 1. 点赞切换接口
export const toggleLike = async (
  data: LikeToggleRequest
): Promise<ApiResponse<string>> => {
  const queryParams = new URLSearchParams();
  queryParams.append('targetId', data.targetId.toString());
  queryParams.append('targetType', data.targetType);

  const response = await api.post(`/interactions/like/toggle?${queryParams.toString()}`);
  return response.data;
};

// 2. 点踩接口
export const dislikeContent = async (
  data: DislikeRequest
): Promise<ApiResponse<string>> => {
  const queryParams = new URLSearchParams();
  queryParams.append('contentId', data.contentId.toString());

  const response = await api.post(`/interactions/dislike?${queryParams.toString()}`);
  return response.data;
};

// 3. 创建评论接口
export const createComment = async (
  data: CreateCommentRequest
): Promise<ApiResponse<string>> => {
  const response = await api.post('/interactions/comments', data);
  return response.data;
};

// 4. 删除评论接口
export const deleteComment = async (
  commentId: number
): Promise<ApiResponse<string>> => {
  const response = await api.delete(`/interactions/comments/${commentId}`);
  return response.data;
};

// 5. 获取评论列表接口
export const getComments = async (
  params: CommentQueryParams
): Promise<ApiResponse<CommentListResponse>> => {
  const queryParams = new URLSearchParams();
  
  queryParams.append('contentId', params.contentId.toString());
  
  if (params.page) {
    queryParams.append('page', params.page.toString());
  }
  
  if (params.size) {
    queryParams.append('size', params.size.toString());
  }
  
  if (params.orderBy) {
    queryParams.append('orderBy', params.orderBy);
  }

  const response = await api.get(`/interactions/comments?${queryParams.toString()}`);
  return response.data;
};

// 6. 查询点赞状态接口
export const getLikeStatus = async (
  data: LikeStatusRequest
): Promise<ApiResponse<boolean>> => {
  const queryParams = new URLSearchParams();
  queryParams.append('targetId', data.targetId.toString());
  queryParams.append('targetType', data.targetType);

  const response = await api.get(`/interactions/like/status?${queryParams.toString()}`);
  return response.data;
}; 